/**
 * 
 */
package com.ekingstar.framework.action;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;

import com.opensymphony.xwork2.ActionSupport;
import com.ekingstar.framework.domain.SystemParameter;
import com.ekingstar.framework.domain.SystemParameter.ParameterType;
import com.ekingstar.framework.service.FunctionExistException;
import com.ekingstar.framework.service.ISystemParameterService;

/**
 * 
 *
 */
@Action(name="addOrUpdate",namespace="/systemParameter")
@Result(name="list",value="list",type=ServletActionRedirectResult.class,params={"namespace","/systemParameter"})
public class AddOrUpdateSystemParameterAction extends ActionSupport{
	
	private SystemParameter systemParameter;
	private ISystemParameterService systemParameterService;
	private Map types;
	private Map parents;
	public Map getTypes() {
		return types;
	}

	public void setTypes(Map types) {
		this.types = types;
	}

	public Map getParents() {
		return parents;
	}

	public void setParents(Map parents) {
		this.parents = parents;
	}

	public SystemParameter getSystemParameter() {
		return systemParameter;
	}

	public void setSystemParameter(SystemParameter systemParameter) {
		this.systemParameter = systemParameter;
	}

	public void setSystemParameterService(
			ISystemParameterService systemParameterService) {
		this.systemParameterService = systemParameterService;
	}

	public String input(){
		if (systemParameter == null || systemParameter.getId() == null){
			
		}
		else {
			systemParameter = systemParameterService.getSystemParameterById(systemParameter.getId());
		}
		ParameterType[] pts = ParameterType.values();
		if (types == null) types = new LinkedHashMap();
		//types.put(null, "请选择");
		for (int i =0 ;i < pts.length;i++){
			types.put(pts[i].name(), pts[i].toString());
		}
		//
		if (parents == null) parents = new LinkedHashMap();
		parents.put(null, "根");
		List allSps = systemParameterService.getAllSystemParameters();
		if (allSps != null && !allSps.isEmpty()){
			for (int i =0 ;i < allSps.size();i++){
				SystemParameter sp =(SystemParameter)allSps.get(i);
				getStructSp(sp,parents,"");
			}
		}
		return "input";
	}
	private void getStructSp(SystemParameter sp,Map p,String preSplit){
		String currentSpilt = "--"+preSplit;
		if (sp != null ){
			p.put(sp.getId(), currentSpilt+sp.getName());
		}
		if (sp.getChildren() != null && !sp.getChildren().isEmpty()){
			for (int i = 0; i < sp.getChildren().size();i++){
				SystemParameter tsp  =(SystemParameter)sp.getChildren().get(i);
				getStructSp(tsp,p,currentSpilt);
			}
		}
	}
	
	public String addOrUpdate(){
		try{
			systemParameterService.addOrUpdateSystemParameter(systemParameter);
		}catch(FunctionExistException e){
			this.addActionError(e.getMessage());
			return "input";
		}
		return "list";
	}
	
}
